//
//  HomeBannerCollectionViewCell.swift
//  LeTu
//
//  Created by g1 on 2024/1/10.
//

import UIKit
import JXBanner

class HomeBannerCollectionViewCell: UICollectionViewCell {
    
    private var banners = [ScenicModel]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    static func getCellHeight() -> CGFloat {
        return HomeBannerItemCollectionViewCell.getCellHieght() + 12+14 + 12+52 + 12+12 + 20
    }
    func setBanners(_ banners: [ScenicModel]) {
        self.banners = banners
        converflowBanner.reloadView()
        
        guard let scenicModel = banners.first else { return }
        setScenicModel(scenicModel)
    }
    func setScenicModel(_ scenicModel: ScenicModel) {
        
        // 设置景区等级
        let scenicLevelText = "\(scenicModel.scenicLevel)A"
        scenicLevelLabel.text = scenicLevelText
        scenicLevelLabel.snp.updateConstraints { make in
            make.width.equalTo(scenicLevelText.widthWithLimit(CGSize(width: CGFloat.greatestFiniteMagnitude, height: 14), font: kSetPingFangRegular(11))+10)
        }
        
        // 景区介绍
        introduceLabel.text = LanguageManager.shared.currentLanguage == .en ? scenicModel.englishBrief : scenicModel.brief
        
        // 设置景区名称
        nameLabel.text = LanguageManager.shared.currentLanguage == .en ? scenicModel.englishName:scenicModel.name
        
        // 设置景区点赞数
        zanLabel.attributed.text = "\(.image(UIImage(named: scenicModel.hasCollected ? "collect_zan" : "collect_unzan")!, .custom(.offset(CGPoint(x: 0, y: -1)), size: CGSize(width: 14, height: 14)))) \(scenicModel.collections)"
        
        // 讲解数
        audioCountLabel.attributed.text = "\(.image(UIImage(named: "play_icon")!, .custom(.offset(CGPoint(x: 0, y: -2)), size: CGSize(width: 13, height: 13)))) \(scenicModel.storyNum)"+"处讲解".localized
        
        // 收听数
        listenCountLabel.attributed.text = "\(.image(UIImage(named: "listeners_count_black_icon")!, .custom(.offset(CGPoint(x: 0, y: -2)), size: CGSize(width: 13, height: 13)))) \(scenicModel.listeners)"+"人已听".localized
        
    }
    
    // MARK: - configUI
    func configUI() {
        
        contentView.addSubview(converflowBanner)
        contentView.addSubview(scenicContentView)
        scenicContentView.addSubview(scenicLevelLabel)
        scenicContentView.addSubview(nameLabel)
        scenicContentView.addSubview(zanLabel)
        scenicContentView.addSubview(audioCountLabel)
        scenicContentView.addSubview(listenCountLabel)
        scenicContentView.addSubview(introduceLabel)
        
        converflowBanner.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(0)
            make.height.equalTo(248)
        }
        scenicContentView.snp.makeConstraints { make in
            make.height.equalTo(20)
            make.top.equalTo(converflowBanner.snp.bottom).offset(12)
            make.left.equalTo(10)
            make.right.equalTo(-10)
        }
        scenicLevelLabel.snp.makeConstraints { make in
            make.left.centerY.equalToSuperview()
            make.width.equalTo(0)
            make.height.equalTo(14)
        }
        zanLabel.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(scenicLevelLabel.snp.right).offset(6)
            make.centerY.equalToSuperview()
            make.right.lessThanOrEqualTo(zanLabel.snp.left).offset(-6)
        }
        introduceLabel.snp.makeConstraints { make in
            make.top.equalTo(nameLabel.snp.bottom).offset(12)
            make.left.right.equalTo(scenicContentView)
            make.height.lessThanOrEqualTo(52)
        }
        audioCountLabel.snp.makeConstraints { make in
            make.left.equalTo(scenicContentView)
            make.top.equalTo(introduceLabel.snp.bottom).offset(12)
            make.height.equalTo(14)
        }
        listenCountLabel.snp.makeConstraints { make in
            make.right.equalTo(scenicContentView)
            make.height.centerY.equalTo(audioCountLabel)
        }
        
    }
    private lazy var converflowBanner: JXBanner = { [weak self] in
        let banner = JXBanner()
        banner.indentify = "converflowBanner"
        banner.delegate = self
        banner.dataSource = self
        banner.clipsToBounds = true
        return banner
    }()
    private lazy var scenicContentView = UIView()
    private lazy var scenicLevelLabel: UILabel = {
        let scenicLevelLabel = UILabel()
        scenicLevelLabel.textColor = .app_white
        scenicLevelLabel.font = kSetPingFangRegular(11)
        scenicLevelLabel.textAlignment = .center
        scenicLevelLabel.backgroundColor = UIColor.hex(hexString: "#9C4615")
        scenicLevelLabel.layer.cornerRadius = 7
        scenicLevelLabel.layer.masksToBounds = true
        return scenicLevelLabel
    }()
    private var nameLabel: UILabel = {
        let nameLabel = UILabel()
        nameLabel.font = kSetPingFangRegular(15)
        return nameLabel
    }()
    private var zanLabel: UILabel = {
        let zanLabel = UILabel()
        zanLabel.font = kSetPingFangRegular(12)
        zanLabel.textAlignment = .right
        return zanLabel
    }()
    private var introduceLabel: UILabel = {
        let introduceLabel = UILabel()
        introduceLabel.font = kSetPingFangRegular(12)
        introduceLabel.textColor = UIColor.hex(hexString: "A1A1A1")
        introduceLabel.numberOfLines = 0
        return introduceLabel
    }()
    private lazy var audioCountLabel: UILabel = {
        let audioCountLabel = UILabel()
        audioCountLabel.textColor = UIColor.hex(hexString: "#464B52")
        audioCountLabel.font = kSetPingFangRegular(12)
        return audioCountLabel
    }()
    private lazy var listenCountLabel: UILabel = {
        let listenCountLabel = UILabel()
        listenCountLabel.textColor = UIColor.hex(hexString: "#464B52")
        listenCountLabel.font = kSetPingFangRegular(12)
        listenCountLabel.textAlignment = .right
        return listenCountLabel
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension HomeBannerCollectionViewCell: JXBannerDataSource {
    
    func jxBanner(_ banner: JXBannerType) -> (JXBannerCellRegister) {
        return JXBannerCellRegister(type: HomeBannerItemCollectionViewCell.self, reuseIdentifier: "ConverflowBannerCell")
    }
    
    func jxBanner(numberOfItems banner: JXBannerType) -> Int { return banners.count }
    
    func jxBanner(_ banner: JXBannerType,
                  cellForItemAt index: Int,
                  cell: UICollectionViewCell)
        -> UICollectionViewCell {
            
            let tempCell = cell as! HomeBannerItemCollectionViewCell
            tempCell.setStoryPic(banners[index].picUrl)
            return tempCell
    }
    
    func jxBanner(_ banner: JXBannerType, params: JXBannerParams) -> JXBannerParams {
        
        return params
            .timeInterval(TimeInterval(5))
            .cycleWay(.forward)
            .isAutoPlay(true)
            .isShowPageControl(false)
        
    }
    
    func jxBanner(_ banner: JXBannerType, layoutParams: JXBannerLayoutParams) -> JXBannerLayoutParams {
        
        return layoutParams
            .itemSize(CGSize(width: screenWidth-20, height: HomeBannerItemCollectionViewCell.getCellHieght()))
            .itemSpacing(2)
            .maximumAngle(1.0)
            .rateHorisonMargin(1.0)
            .minimumAlpha(0.8)

    }
    
}

//MARK:- JXBannerDelegate
extension HomeBannerCollectionViewCell: JXBannerDelegate {
    
    public func jxBanner(_ banner: JXBannerType, didSelectItemAt index: Int) {
        guard index < banners.count else { return }
        send(event: HomeVC.TransferEvent.clickScenic(banners[index]))
    }
    
    func jxBanner(_ banner: JXBannerType, coverView: UIView) {
//        let title = UILabel()
//        title.frame = CGRect(x: 0, y: 0, width: 100, height: 30)
//        title.text = ""
//        title.textColor = UIColor.red
//        title.font = UIFont.systemFont(ofSize: 16)
//        coverView.addSubview(title)
    }
    
    func jxBanner(_ banner: JXBannerType, center index: Int) {
        
        guard index < banners.count else { return }
        setScenicModel(banners[index])
        
    }
    
}

class HomeBannerItemCollectionViewCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    fileprivate static func getCellHieght() -> CGFloat {
        return 248
    }
    func setStoryPic(_ picUrl: String) {
        // 设置景区图片
        imgView.sd_setImage(with: URL.encodedURL(string: picUrl))
    }
    
    // MARK: - configUI
    func configUI() {
        
        contentView.addSubview(imgView)
        
        imgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
    }
    private(set) lazy var imgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.layer.cornerRadius = 12
        imgView.layer.masksToBounds = true
        imgView.backgroundColor = UIColor.hex(hexString: "#F9F9F9")
        return imgView
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

